﻿using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Topro.ConsoleApp
{
    class Program
    {
        /// <summary>
        /// FTP方式上传 
        /// </summary>
        public static int UploadFtp(string filePath, string filename, string serverIP, string userName, string userPass)
        {



            //文件路径
            var fileName = $"{filePath}\\{filename}";
            //FileInfo
            var fileInfo = new FileInfo(fileName);
            //Ftp地址
            string uri = $"ftp://{serverIP}/{fileInfo.Name}";
            FtpWebRequest reqFTP;
            // Create FtpWebRequest object from the Uri provided 
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            try
            {
                // Provide the WebPermission Credintials 
                reqFTP.Credentials = new NetworkCredential(userName, userPass);

                // By default KeepAlive is true, where the control connection is not closed 
                // after a command is executed. 
                reqFTP.KeepAlive = false;

                // Specify the command to be executed. 
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                // Specify the data transfer type. 
                reqFTP.UseBinary = true;

                // Notify the server about the size of the uploaded file 
                reqFTP.ContentLength = fileInfo.Length;

                // The buffer size is set to 2kb 
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
                //FileStream fs = fileInf.OpenRead(); 
                FileStream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                // Stream to which the file to be upload is written 
                Stream strm = reqFTP.GetRequestStream();

                // Read from the file stream 2kb at a time 
                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends 
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream 
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream 
                strm.Close();
                fs.Close();
                return 0;
            }
            catch (Exception ex)
            {
                reqFTP.Abort();
                //  Logging.WriteError(ex.Message + ex.StackTrace);
                return -2;
            }
        }

        //private static readonly Logger logger = LogManager.GetLogger("OperLog");
        ///// <summary>
        ///// NLog日志对象
        ///// </summary>
        //private static readonly Logger httpLog = LogManager.GetLogger("HttpLog");
        static void Main(string[] args)
        {
            //UploadFtp(@$"D:\work\dotnet\Topro.IQS\Topro.ConsoleApp\bin\Debug\net5.0\", "101.txt", "10.10.14.236", "", "");

            var reg = new Regex(@"^[0-9]{2}:\d{4}$");
            if (!reg.IsMatch("02:1124"))
            {
                throw new Exception("格式有误.");
            }

            var content = "22-0119-114527";
            //当前时期
            //var now = "114527".ToString("HHmmsss");yyMMdd

            DateTime dt = DateTime.ParseExact("0119114527", "MMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);

            //输入时期
            var reqeust = Convert.ToDateTime(content).Date;
        }
    }
}
